package com.doitnow;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity implements OnClickListener,
		android.content.DialogInterface.OnClickListener, OnItemClickListener {

	private TaskEngine FEngine;
	private EditText edtName;
	private ListView lvlist;
	private Timer FTimer;
	private TimerTask FTask;
	private MediaPlayer mp;

	private void ShowListView(ListView list) {
		Cursor c = FEngine.GetCursor();
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_expandable_list_item_2, c,
				new String[] { c.getColumnName(1), c.getColumnName(2) },
				new int[] { android.R.id.text1, android.R.id.text2 });
		list.setAdapter(simpleCursorAdapter);
	};

	private void ShowFinishedListView(ListView list) {
		Cursor c = FEngine.GetFinishedCursor();
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_expandable_list_item_2, c,
				new String[] { c.getColumnName(1), c.getColumnName(2) },
				new int[] { android.R.id.text1, android.R.id.text2 });
		list.setAdapter(simpleCursorAdapter);
	};
	
	private void BroadcastRefresh(){
		Intent intent = new Intent();  
        intent.setAction("com.DoitNow.DoWidgetProvider.Refresh");  
        intent.putExtra("value", FEngine.GetTaskInfo());  
        MainActivity.this.sendBroadcast(intent); 
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		edtName = (EditText) findViewById(R.id.edtName);
		mp = MediaPlayer.create(MainActivity.this, R.raw.time);

		// 连接数据库
		FEngine = new TaskEngine();
		FEngine.open(this);

		// 绑定XML中的ListView，作为Item的容器
		lvlist = (ListView) findViewById(R.id.MyListView);

		// 显示列表
		ShowListView(lvlist);

		// ListView上添加点击菜单
		ListView.OnCreateContextMenuListener MenuLis = new ListView.OnCreateContextMenuListener() {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				// 添加菜单项
				menu.add(Menu.NONE, 1, 1, "执行");
				menu.add(Menu.NONE, 2, 2, "标记完成");
				menu.add(Menu.NONE, 3, 3, "删除");
				menu.add(Menu.NONE, 4, 4, "查看已完成");
				menu.add(Menu.NONE, 5, 5, "取消");
			}
		};
		lvlist.setOnCreateContextMenuListener(MenuLis);
		lvlist.setOnItemClickListener(this);

		// 增加添加按钮
		Button btnAdd = (Button) findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(this);
	}

	protected void onPause() {
		super.onPause();
		mp.stop();
		BroadcastRefresh();
	}
	
    protected void onStop() {
    	super.onStop();
    	mp.stop();
    	BroadcastRefresh();
	} 	
	
	// 选中菜单Item后触发
	public boolean onContextItemSelected(MenuItem item) {

		AdapterView.AdapterContextMenuInfo menuInfo;
		menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

		int selectid = item.getItemId();
		if (selectid == 1) {
			// 选择时间块
			AlertDialog.Builder builder = new AlertDialog.Builder(this);

			// 设置Dialog的标题
			builder.setTitle("选择时间块");
			// 设置可供选择的ListView
			builder.setItems(new String[] { "40分钟", "20分钟" }, this);

			AlertDialog ad = builder.create();

			ad.show();

		} else if (selectid == 2) {
			FEngine.finishtask((int) menuInfo.id);
			ShowListView(lvlist);
		} else if (selectid == 3) {
			FEngine.deltask((int) menuInfo.id);
			ShowListView(lvlist);
		} else if (selectid == 4) {
			ShowFinishedListView(lvlist);
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onClick(View arg0) {
		FEngine.addtask(edtName.getText().toString());
		ShowListView(lvlist);
	}

	@Override
	public void onClick(DialogInterface arg0, int arg1) {
		int max;
		if (arg1 == 0)
			max = 60 * 20 * 1000;
		else
			max = 60 * 40 * 1000;
		FTimer = new Timer();
		FTask = new TimerTask() {
			@Override
			public void run() {
				try {
					mp.prepare();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				mp.start();

			}
		};
		FTimer.schedule(FTask, max);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (mp.isPlaying()) {
			mp.stop();
		}
	}

}
